package com.ctrip.ec.api.admin.controller.ad;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.biz.dao.image.ImageDao;
import com.ctrip.ec.biz.image.ImageUpload;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.ad.AdDao;
import com.ctrip.ec.biz.entity.ad.AdEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
@RestController
@RequestMapping("ad")
public class AdController{
    @Autowired
    private AdDao ad;

    @Value("${imageDomain}")
    private  String imageDomain;

    @Autowired
    private ImageDao image;

    @GetMapping("list")
    public List<AdEntity> list() throws SQLException {
        List<AdEntity> list = ad.list();
        return list;
    }

    @PostMapping("list")
    public List<AdEntity> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        List<AdEntity> list = ad.list(setting);
//        for (AdEntity entity : list){
//            entity.setImageUrl(imageDomain + entity.getImageUrl());
//        }
        RequestContext.setResponseHeaderCount(setting.getRecordCount());
        return list;
    }

    @GetMapping("detail/{id}")
    public  AdEntity detail(@PathVariable Long id) throws SQLException {
        return ad.entity(id);
    }

    @PostMapping("save")
    public  Long save(@RequestBody AdEntity entity) throws SQLException {
        ad.save(entity);
        image.inUse(entity.getImageId());
        return entity.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {
        List<Long> imageIds = ad.getImageIds(ids);
        image.unUse(imageIds);
        ad.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        ad.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        ad.unerase(ids);
    }
}
